home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8155 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: more problems with qsort
  5. Date: 1 Mar 1996 17:36:32 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4h81m0$avr@solutions.solon.com>
  8. References: <177399702S86.JW1675A@american.edu> <4h0j9e$ng5@clarknet.clark.net>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <4h0j9e$ng5@clarknet.clark.net>, yom <yom@clark.net> wrote:
  12. >Since you're trying to sort a char**, the qsort function call must
  13. >look like this:
  14.  
  15. >qsort(array,lines,sizeof(char **),(int (*)(void *,void *)) compare);
  16.  
  17. This is entirely wrong.  If you're trying to sort an array of pointers
  18. to char (or a char ** acting like an array of char *'s), you must use
  19.     qsort(ary, nelem, sizeof(array[0]), compare);
  20. ... and array[0] will be a char *, which may be different from a
  21. char **.
  22.  
  23. >And your compare function must be defined like this:
  24.  
  25. >int compare(char **a,char **b) {return strcmp(*a,*b);}
  26.  
  27. No, it really mustn't.  It must be defined like this:
  28. int compare(const void *a, const void *b) {
  29.     return strcmp(*(char **) a, *(char **) b);
  30. }
  31.  
  32. >The use of sizeof operator ensures that this code will be compatible
  33. >with 64 bit architecture such as AXP/OSF.
  34.  
  35. Except that you took sizeof() the wrong type, and you cast a function
  36. pointer incorrectly.
  37.  
  38. -s
  39. -- 
  40. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  41. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  42. FUCK the communications decency act.  Goddamned government.  [literally.]
  43. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  44.